<template>
  <div class="xtx-city">
    <div class="select" @click="toggleDialog" :class="{ active }">
      <span class="placeholder" v-if="!seled.fullSels">请选择配送地址</span>
      <span class="value">{{ seled.fullSels }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span
        class="ellipsis"
        @click="selCity(city)"
        v-for="city in cityList"
        :key="city.code"
      >
        {{ city.name }}
      </span>
    </div>
  </div>
</template>
<script>
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'
// 获取城市列表数据
function useCityList () {
  const cityList = ref([])
  // 备份数据
  const backupCityList = []
  const getCityList = async () => {
    const { data } = await axios({ url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json' })
    cityList.value = data
    // 备份数据
    backupCityList.push(...data)
  }
  onMounted(() => {
    getCityList()
  })
  return { cityList, backupCityList }
}
export default {
  name: 'XtcCity',
  // 选择完触发change事件传递数据
  emits: ['change'],
  setup (props, { emit }) {
    // 获取城市列表数据
    const { cityList, backupCityList } = useCityList()
    // 控制选择弹层是否显示
    const active = ref(false)
    // 打开城市选择
    function open () {
      active.value = true
      cityList.value = backupCityList
    }
    // 关闭城市选择
    function close () {
      active.value = false
    }
    function toggleDialog () {
      if (active.value) {
        close()
      } else {
        open()
      }
    }
    /**
     * 点击选择省市区 :
     * 1.默认展示省
     * 2.点击省展示市,点击市展示区
     */
    // 点击选中城市数据
    const seled = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countryCode: '', // 地区code
      countryName: '', // 地区名
      // 最终选择(省+市+区)
      fullSels: ''
    })
    const selCity = (city) => {
      // 联动实现
      cityList.value = city.areaList
      // 存储选择市省市区数据
      if (city.level === 0) { // 省
        seled.provinceName = city.name
        seled.provinceCode = city.code
      } else if (city.level === 1) { // 市
        seled.cityName = city.name
        seled.cityCode = city.code
      } else { // 区
        seled.countryName = city.name
        seled.countryCode = city.code
        // 拼接完整地址
        seled.fullSels = seled.provinceName + ' ' + seled.cityName + ' ' + seled.countryName
        // console.log(seled.countryCode, 11111111111)
        // 选择数据传递出去
        emit('change', seled)
        // 选完关闭弹层
        toggleDialog()
      }
    }
    return { active, toggleDialog, cityList, selCity, seled }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
